/**
 * Created by mac on 17/6/21.
 */
var request = require("request");
var API = function (){};
var baseUrl = 'http://localhost:8080';
module.exports = API;

API.ajax = {
    post:function (req,success) {
        var url = baseUrl + req.originalUrl;
        var j = request.jar();
        var cookie;
        for(var val in req.cookies){
            cookie = request.cookie(val+'='+req.cookies[val]);
            j.setCookie(cookie, url);
        }
        var options = {
            url: url,
            method: 'POST',
            jar:j,
            json: true,
            headers: {
                "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"
            },
            form: req.body
        };
        request(options , success);
    },
    get:function (req,success) {
        var url = baseUrl + req.originalUrl;
        var j = request.jar();
        var cookie;
        for(var val in req.cookies){
            cookie = request.cookie(val+'='+req.cookies[val]);
            j.setCookie(cookie, url);
        }
        var options = {
            url: url,
            method: 'GET',
            jar:j,
            json: true,
            headers: {
                "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"
            }
        };
        request(options , success);
    }
}